﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CRM.Web.Admin.Controls
{
    public partial class Pager : System.Web.UI.UserControl
    {
        private int _currentPageIndex;
        private int _pageSize;
        private int _itemCount;
        private int _pageCount;
        private string _urlFormat;
        /// <summary>
        /// 当前页码
        /// </summary>    
        public int CurrentPageIndex
        {
            get
            {
                return _currentPageIndex;
            }
            set
            {
                _currentPageIndex = value;
            }
        }

        /// <summary>
        /// 页长度
        /// </summary>
        public int PageSize
        {
            get
            {
                return _pageSize;
            }
            set
            {
                _pageSize = value;
            }
        }

        /// <summary>
        /// 总数
        /// </summary>
        public int ItemCount
        {
            get
            {
                return _itemCount;
            }
            set
            {
                _itemCount = value;
            }
        }

        /// <summary>
        /// URL地址格式
        /// </summary>
        public string urlFormat
        {
            get
            {
                return _urlFormat;
            }
            set
            {
                _urlFormat = value;
            }
        }

        /// <summary>
        /// 两边显示的个数
        /// </summary>
        public int PageCount
        {
            get
            {
                return _pageCount;
            }
            set
            {
                _pageCount = value;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (ItemCount <= PageSize)
                return;
            int sumPage = (ItemCount + PageSize - 1) / PageSize;

            //以CurrentPageIndex为中心，前台显示pageCount个页码导航
            int start = CurrentPageIndex - PageCount;
            int end = CurrentPageIndex + PageCount;

            if (sumPage > (PageCount * 2 - 1))
            {
                if (start < 1)
                {
                    start = 1;
                    end = start + 2 * PageCount;
                }
                else if (end > sumPage)
                {
                    start = sumPage - 2 * PageCount;
                    end = sumPage;
                }
            }
            else
            {
                start = 1;
                end = sumPage;
            }

            string tmp = "<a href=\"" + urlFormat + "\">{0}</a>";
            System.Text.StringBuilder sb = new System.Text.StringBuilder("<div id='pager'>");

            if (CurrentPageIndex > 1)
            {
                sb.AppendFormat("<a href=\"" + urlFormat + "\">{1}</a>", CurrentPageIndex - 1, Server.HtmlDecode("< Prev"));
                if (start > 1)
                {
                    sb.AppendFormat("<a href=\"" + urlFormat + "\">{0}</a>... ", 1);
                }
            }
            for (int i = start; i <= end; i++)
            {
                if (i == CurrentPageIndex)
                    sb.Append("<span class='current'>" + CurrentPageIndex.ToString() + "</span>");
                else
                    sb.AppendFormat(tmp, i);
            }
            if (CurrentPageIndex < sumPage)
            {
                if (end < sumPage)
                {
                    sb.AppendFormat("... <a href=\"" + urlFormat + "\">{0}</a>", sumPage);
                }
                sb.AppendFormat("<a href=\"" + urlFormat + "\">Next ></a>", CurrentPageIndex + 1);
            }

            writer.Write(sb.Append("</div>").ToString());
        }   
    }
}